package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.example.administrator.myapplication.enity.HttpResult;
import com.example.administrator.myapplication.enity.Subject;

import java.util.List;

import okhttp3.internal.http.HttpMethod;
import rx.Subscriber;

public class MainActivity extends AppCompatActivity {
    String TAG = "<<<";
    private TextView resultTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.click_me_BN);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getNet();
            }
        });

        resultTV = (TextView) findViewById(R.id.result_TV);
    }

    Subscriber subscriber = new Subscriber<HttpResult<List<Subject>>>() {
        @Override
        public void onCompleted() {
            Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Throwable e) {
            resultTV.setText(e.getMessage());
        }

        @Override
        public void onNext(HttpResult<List<Subject>> movieEntity) {
            resultTV.setText(movieEntity.toString());
        }
    };
    public void getNet() {


        HttpMethods.getInstance().getTopMovie(subscriber, 0, 10);

        String baseUrl = "https://api.douban.com/v2/movie/";
//二
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(baseUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .build();
//
//        MoviceService moviceService = retrofit.create(MoviceService.class);
//        moviceService.getTopMovie(0, 10)
//                .subscribeOn(Schedulers.io())// 指定 subscribe() 发生在 IO 线程
//                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
//                .subscribe(new Subscriber<MovieEntity>() {
//                    @Override
//                    public void onCompleted() {
//                        Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        resultTV.setText(e.getMessage());
//                    }
//
//                    @Override
//                    public void onNext(MovieEntity movieEntity) {
//                        resultTV.setText(movieEntity.toString());
//                    }
//                })

        ;

        //一
//        Call<MovieEntity> topMovie = moviceService.getTopMovie(0, 10);
//
//        topMovie.enqueue(new Callback<MovieEntity>() {
//            @Override
//            public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {
//                Log.e(TAG, "onResponse: " + "success" + response.toString());
//            }
//
//            @Override
//            public void onFailure(Call<MovieEntity> call, Throwable t) {
//                Log.e(TAG, "onResponse: " + "onFailure" + call.toString());
//            }
//        });
    }

}
